unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, ExtCtrls,About_us;

type
  TForm1 = class(TForm)
    pnl1: TPanel;
    lbl1: TLabel;
    edt_name: TEdit;
    lbl2: TLabel;
    rb1: TRadioButton;
    rb2: TRadioButton;
    lbl3: TLabel;
    dtp1: TDateTimePicker;
    lbl4: TLabel;
    edt_gs: TEdit;
    lbl5: TLabel;
    edt_add: TEdit;
    lbl6: TLabel;
    edt_Tel: TEdit;
    edt_QQ: TEdit;
    lbl7: TLabel;
    lbl8: TLabel;
    lbl9: TLabel;
    lv1: TListView;
    mmo_re: TMemo;
    btn1: TButton;
    btn_update: TButton;
    btn_delete: TButton;
    btn4: TButton;
    btn5: TButton;
    lbl11: TLabel;
    lbl12: TLabel;
    lbl13: TLabel;
    lbl10: TLabel;
    lbl14: TLabel;
    lbl15: TLabel;
    lbl16: TLabel;
    lbl17: TLabel;
    lbl18: TLabel;
    lbl19: TLabel;
    btn_import: TButton;
    dlgOpen1: TOpenDialog;
    dlgSave1: TSaveDialog;
    lbl_about: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure btn4Click(Sender: TObject);
    procedure edt_TelKeyPress(Sender: TObject; var Key: Char);
    procedure edt_QQKeyPress(Sender: TObject; var Key: Char);
    procedure btn1Click(Sender: TObject);
    procedure lv1DblClick(Sender: TObject);
    procedure btn_updateClick(Sender: TObject);
    procedure btn_deleteClick(Sender: TObject);
    procedure btn5Click(Sender: TObject);
    procedure btn_importClick(Sender: TObject);
    procedure lbl_aboutClick(Sender: TObject);
  private
    { Private declarations }
    procedure EnterNumberOnly(var Key: Char);
    procedure IfEmptyCheck(myEdit: TEdit; pMsg: PChar);
    function CheckEmptyAll(): Integer;
    procedure EmptyEditArea();
    function CheckRepeat():Integer;
  public
    { Public declarations }
  end;

const
  MainTitle = 'My Address Books';

var
  Form1: TForm1;
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Caption := MainTitle;
  Font := Screen.IconFont;
  rb1.Checked := True;
  dtp1.DateTime := StrToDateTime('1981/01/01');
end;

procedure TForm1.btn4Click(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TForm1.EnterNumberOnly(var Key: Char);
begin
  if not (Key in ['0'..'9', #8, #127]) then
  begin
    Key := #0;
  end;
end;

procedure TForm1.edt_TelKeyPress(Sender: TObject; var Key: Char);
begin
  EnterNumberOnly(Key);
end;

procedure TForm1.edt_QQKeyPress(Sender: TObject; var Key: Char);
begin
  EnterNumberOnly(Key);
end;

procedure TForm1.IfEmptyCheck(myEdit: TEdit; pMsg: PChar);
begin
  Application.MessageBox(pMsg, pChar('Prompt'), MB_OK + MB_ICONINFORMATION);
  myEdit.SetFocus;
end;

function TForm1.CheckEmptyAll: Integer;
var
  i: Integer;
begin
  i := 0;
  if (edt_name.Text = '') then
  begin
    IfEmptyCheck(edt_name, 'Name cannot null');
    i := 1;
  end
  else if (edt_Tel.Text = '') then
  begin
    IfEmptyCheck(edt_Tel, 'TelphoneNuber cannot null');
    i := 1;
  end
  else if (edt_QQ.Text = '') then
  begin
    IfEmptyCheck(edt_QQ, 'QQ cannot null');
    i := 1;
  end;

  Result := i;
end;

procedure TForm1.btn1Click(Sender: TObject);
var
  i,iRepeat: Integer;    //is it Repeat? repeat:1  no :0
  lt: TListItem;
  uSex: string;
begin
  i := CheckEmptyAll();
  if (i = 0) then
  begin
//    iRepeat:=0;
     iRepeat:=CheckRepeat();

    if(iRepeat=0) then begin


          if rb1.Checked then
            uSex := 'male'
          else if rb2.Checked then
            uSex := 'female';
          lt := lv1.Items.Add;
          lt.Caption := IntToStr(lv1.Items.Count);
          lt.SubItems.Add(edt_name.Text);
          lt.SubItems.Add(uSex);
          lt.SubItems.Add(edt_Tel.Text);
          lt.SubItems.Add(edt_QQ.Text);
          lt.SubItems.Add(DateTimeToStr(dtp1.DateTime));
          lt.SubItems.Add(edt_gs.Text);
          lt.SubItems.Add(edt_add.Text);
          lt.SubItems.Add(mmo_re.Text);

          EmptyEditArea();
      end
      else begin
        Application.MessageBox('input telphone is duplicate', 'prompt', MB_OK + MB_ICONINFORMATION);
        edt_Tel.SetFocus;
      end;

  end;
end;

procedure TForm1.lv1DblClick(Sender: TObject);
var

  i: Integer;
  uSex: string;
begin

    if (lv1.Selected <> nil) then
    begin

      btn_update.Enabled := True;
      btn_delete.Enabled := True;
      i := lv1.ItemIndex;
      with lv1.Items.Item[i].SubItems do
      begin
        edt_name.Text := Strings[0];
        if uSex = 'male' then
          rb1.Checked := True
        else if uSex = 'female' then
          rb2.Checked := True;

        edt_Tel.Text := Strings[2];
        edt_QQ.Text := Strings[3];
        dtp1.DateTime := StrToDateTime(Strings[4]);
        edt_gs.Text := Strings[5];
        edt_add.Text := Strings[6];
        mmo_re.Text := Strings[7];

      end;

    end
    else
    begin
      if (lv1.Items.Count = 0) then
        Application.MessageBox('no data to modify', 'prompt', MB_OK + MB_ICONINFORMATION)
      else
        Application.MessageBox('no data selected', 'prompt', MB_OK + MB_ICONINFORMATION);
    end;


end;

procedure TForm1.btn_updateClick(Sender: TObject);
var
  i,n,iRepeat: Integer;
  uSex: string;
begin
     n:=CheckEmptyAll();
     if(n=0) then begin
        iRepeat:=CheckRepeat();
         if(iRepeat=0) then begin


            if (lv1.Selected <> nil) then
            begin
              i := lv1.ItemIndex;
              if rb1.Checked then
                uSex := 'male'
              else if rb2.Checked then
                uSex := 'female';

              with lv1.Items.Item[i].SubItems do
              begin
                Strings[0] := edt_name.Text;
                Strings[1] := uSex;
                Strings[2] := edt_Tel.Text;
                Strings[3] := edt_QQ.Text;
                Strings[4] := DateTimeToStr(dtp1.DateTime);
                Strings[5] := edt_gs.Text;
                Strings[6] := edt_add.Text;
                Strings[7] := mmo_re.Text;
              end;

              Application.MessageBox('Update OK', 'prompt', MB_OK + MB_ICONINFORMATION);

               EmptyEditArea();

            end;
         end
         else begin
          Application.MessageBox('input telphone is duplicate', 'prompt', MB_OK + MB_ICONINFORMATION);
          edt_Tel.SetFocus;
          end;

     end;
end;

procedure TForm1.btn_deleteClick(Sender: TObject);
var
  i: Integer;
begin
  if (lv1.Items.Count > 0) then  // exist data
    if (lv1.Selected <> nil) then
    begin
      i := lv1.Items.IndexOf(lv1.Selected);
      if Application.MessageBox('Do you want Delete it?', 'Prompt', MB_YESNO + MB_ICONINFORMATION) = mrYes then
      begin

        lv1.Items.Delete(i);
        EmptyEditArea();

      end;

    end;
end;

procedure TForm1.btn5Click(Sender: TObject);
var
  myList: TStringList;
  n, i: Integer;
  str, tem: String;   // str daibiao one Row data
  saveFile:String;
begin

  if lv1.Items.Count > 0 then
  begin      //ensure exist data
  if dlgSave1.Execute then begin    //save file dialog
      saveFile:=dlgSave1.FileName;
    try
      myList := TStringList.Create;    //create list
        //loop  list first to last,
      for n := 0 to lv1.Items.Count - 1 do
      begin
        str := '';
        for i := 0 to lv1.Items.Item[n].SubItems.Count - 1 do
        begin
             //receive template data
          tem := lv1.Items.Item[n].SubItems.Strings[i];
          if (tem = '') then
            tem := '###';
          if (str = '') then
            str := tem
          else
            str := str + '+' + tem;
        end;

        myList.Add(str);

      end;
      ShowMessage(myList.Text);
      myList.SaveToFile(saveFile);
    finally
      myList.Free;  //Release memory
    end;
  end
  else
  begin
    Application.MessageBox('no data to save!', 'Prompt', MB_OK + MB_ICONINFORMATION);
  end;
  end;

end;



//import address books
procedure TForm1.btn_importClick(Sender: TObject);
var
  myList:TStringList;
  lsList:TStringList;
  i,ln,n:Integer;
  lxrStr,
  temStr:String;
  lsItem:TListItem;
  openFile:String;
begin

  if dlgOpen1.Execute then begin
        openFile:=dlgOpen1.FileName;
    if FileExists(openFile) then begin



          try
            lv1.Items.Clear;
            myList:=TStringList.Create;
            lsList:=TStringList.Create;

            //loadFile from path
            myList.LoadFromFile(dlgOpen1.FileName);
        //    ShowMessage(myList.Text);

            for i:=0 to myList.Count-1 do begin
              lxrStr:=myList.Strings[i];
        //      ShowMessage(lxrStr);
              while(Pos('+',lxrStr)>0) do begin
                ln:=Pos('+',lxrStr);
                temStr:=Copy(lxrStr,1,ln-1);  //Intercept string to list
        //        ShowMessage(temStr);
                lsList.Add(temStr);

                Delete(lxrStr,1,ln);
        //        ShowMessage(lxrStr);
              end;
              lsList.Add(lxrStr);  // add last field;

              lsItem:=lv1.Items.Add;
              lsItem.Caption:=IntToStr(lv1.Items.Count);

              for n:=0 to lsList.Count-1 do begin
                lsItem.SubItems.Add(lsList.Strings[n]);
              end;

              lsList.Clear;
            end;


          finally
            myList.Free;
            lsList.Free;
          end;   
        end;
      end;


end;

procedure TForm1.EmptyEditArea;
begin
 edt_name.Text := '';
        rb1.Checked := True;
        edt_Tel.Text := '';
        edt_QQ.Text := '';
        dtp1.DateTime := StrToDateTime('1981/01/01');
        edt_gs.Text := '';
        edt_add.Text := '';
        mmo_re.Text := '';

          //disable button
        btn_update.Enabled := False;
        btn_delete.Enabled := False;
end;

procedure TForm1.lbl_aboutClick(Sender: TObject);
begin
  Form2:= TForm2.Create(Application);
  Form2.ShowModal;
end;

function TForm1.CheckRepeat: Integer;
var
  i:Integer;
  iRep:Integer;
begin
  iRep:=0;
   if(lv1.Items.Count>0) then begin

      for i:=0 to lv1.Items.Count-1 do begin
        if (lv1.Items.Item[i].SubItems.Strings[2]=edt_Tel.Text) then begin
          iRep:=1;  //repeated,put flag set 1

          Break;
        end;
      end;
    end;
    result:= iRep;
end;

end.

